package org.wuhong.client;

import javax.lang.model.element.VariableElement;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @ClassName TestCommunicationClient
 * @Description TODO
 * @Author director吴
 * @Date 2022/1/5 13:16
 * @Version 1.0
 **/
public class TestCommunicationClient {

    public static void main(String[] args) throws Exception {

        //得到网络通道
        SocketChannel socketChannel = SocketChannel.open();

        //设置非阻塞模式
        socketChannel.configureBlocking(false);

        //连接服务器
        boolean connect = socketChannel.connect(new InetSocketAddress("127.0.0.1", 6666));

        //连接不成功 也不阻塞
        if (!connect){
            while (!socketChannel.finishConnect()){
                System.out.println("客户端不阻塞");
            }

        }

        //连接成功发送数据
        String aa = "正在申请连接";

        //不用指定buffer大小,将其大小设置和字符串一样大
        ByteBuffer buffer = ByteBuffer.wrap(aa.getBytes());

        socketChannel.read(buffer);

        System.in.read();



    }
}
